package com.pearstack.sonic.core.protocol;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * String 消息协议
 *
 * @author 就眠儀式
 * */
public class StringProtocol implements Protocol<String>{

    @Override
    public String encode(ByteBuffer byteBuffer) {

        byte[] bytes = new byte[byteBuffer.remaining()];

        byteBuffer.get(bytes);

        return new String(bytes, StandardCharsets.UTF_8);
    }

    @Override
    public ByteBuffer decode(String object) {

        byte[] bytes = object.getBytes(StandardCharsets.UTF_8);

        return ByteBuffer.wrap(bytes);
    }
}
